import Vue from "vue";
import VueRouter from "vue-router";

import userUtils from "@/utils/userUtil";

Vue.use(VueRouter);

const routes = [
  {
    // 首页路由，进入首页
    path: "/",
    title: "PTE/CCL培训_九炸炸PTE学习平台_A WIN留学移民专家_EASTON悉尼雅思教育集团(EASTON九炸炸英语)",
    component: () => import(/* webpackChunkName: "home" */ "../views/Home.vue"),
    children: [
      {
        path: "",
        meta: {
          title: "PTE/CCL培训_九炸炸PTE学习平台_A WIN留学移民专家_EASTON悉尼雅思教育集团(EASTON九炸炸英语)",
          content:{
            keywords:'EASTON九炸炸英语,PTE/CCL培训,九炸炸PTE学习平台,A WIN留学移民专家,EASTON悉尼雅思教育集团',
            description:'Easton九炸炸PTE（原悉尼雅思），成立于2012年，于2015年开始培训PTE，作为悉尼第一家PTE培训学校，我们是PTE评分系统的探索先驱，是PTE教学研究的开拓者，更是PTE真题预测的奠基者。多年的时间历练，让只用通过率说话的Easton，凭借其教学质量和教学成果，每年帮助上千名学生达成语言目标。2017年创立A WIN留学移民专家服务，恪守倾听，关心，专业，负责的态度，为学生的留学移民梦，填上了最后一块拼图。',
          },
        },
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/courses")
      },
      {
        path: "/courses",
        meta: {
          title: "PTE/CCL培训_九炸炸PTE学习平台_A WIN留学移民专家_EASTON悉尼雅思教育集团(EASTON九炸炸英语)",
          content:{
            keywords:'EASTON九炸炸英语,PTE/CCL培训,九炸炸PTE学习平台,A WIN留学移民专家,EASTON悉尼雅思教育集团',
            description:'Easton九炸炸PTE（原悉尼雅思），成立于2012年，于2015年开始培训PTE，作为悉尼第一家PTE培训学校，我们是PTE评分系统的探索先驱，是PTE教学研究的开拓者，更是PTE真题预测的奠基者。多年的时间历练，让只用通过率说话的Easton，凭借其教学质量和教学成果，每年帮助上千名学生达成语言目标。2017年创立A WIN留学移民专家服务，恪守倾听，关心，专业，负责的态度，为学生的留学移民梦，填上了最后一块拼图。',
          },
        },
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/courses")
      },

      {
        path: "/ptecourse",
        meta: {
          title: "PTE"
        },
        component: () =>
          import(
            /* webpackChunkName: "courses" */ "@/views/courses/ptecourseinfo"
          )
      },
      {
        path: "/cclcourseinfo",
        meta: {
          title: "课程详情"
        },
        component: () =>
          import(
            /* webpackChunkName: "coursesinfo" */ "@/views/courses/cclcourseinfo"
          )
      },
      {
        path: "/ptebase",
        meta: {
          title: "PTE"
        },
        component: () =>
          import(/* webpackChunkName: "courses" */ "@/views/courses/ptebase")
      },
      {
        path: "/ptecourseinfo",
        meta: {
          title: "课程详情"
        },
        component: () =>
          import(
            /* webpackChunkName: "courses" */ "@/views/courses/ptecourseinfo"
          )
      },
      {
        path: "/basecourselist",
        meta: {
          title: "课程列表"
        },
        component: () =>
          import(
            /* webpackChunkName: "courseslist" */ "@/views/courses/basecourselist"
          )
      },
      {
        path: "/practice",
        meta: {
          title: "练习平台",
          content:{
            keywords:'EASTON九炸炸英语,PTE/CCL培训,九炸炸PTE学习平台,A WIN留学移民专家,EASTON悉尼雅思教育集团',
            description:'Easton九炸炸PTE（原悉尼雅思），成立于2012年，于2015年开始培训PTE，作为悉尼第一家PTE培训学校，我们是PTE评分系统的探索先驱，是PTE教学研究的开拓者，更是PTE真题预测的奠基者。多年的时间历练，让只用通过率说话的Easton，凭借其教学质量和教学成果，每年帮助上千名学生达成语言目标。2017年创立A WIN留学移民专家服务，恪守倾听，关心，专业，负责的态度，为学生的留学移民梦，填上了最后一块拼图。',
          },
        },
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/practice")
      },
      {
        path: "/community",
        meta: {
          title: "社区资讯",
          content:{
            keywords:'EASTON九炸炸英语,PTE/CCL培训,九炸炸PTE学习平台,A WIN留学移民专家,EASTON悉尼雅思教育集团',
            description:'Easton九炸炸PTE（原悉尼雅思），成立于2012年，于2015年开始培训PTE，作为悉尼第一家PTE培训学校，我们是PTE评分系统的探索先驱，是PTE教学研究的开拓者，更是PTE真题预测的奠基者。多年的时间历练，让只用通过率说话的Easton，凭借其教学质量和教学成果，每年帮助上千名学生达成语言目标。2017年创立A WIN留学移民专家服务，恪守倾听，关心，专业，负责的态度，为学生的留学移民梦，填上了最后一块拼图。',
          },
        },
        component: () =>
          import(/* webpackChunkName: "community" */ "@/views/community")
      },
      {
        path: "/community/articleDetail",
        meta: {
          title: "文章详情"
        },
        component: () =>
          import(
            /* webpackChunkName: "community" */ "@/views/community/article"
          )
      },
      // {
      //   path: "/logedpractice",
      //   component: () =>
      //     import(/* webpackChunkName: "home" */ "@/views/practice/loged")
      // },
      {
        path: "/loged",
        name: "loged",
        meta: {
          title: "练习平台",
          content:{
            keywords:'EASTON九炸炸英语,PTE/CCL培训,九炸炸PTE学习平台,A WIN留学移民专家,EASTON悉尼雅思教育集团',
            description:'Easton九炸炸PTE（原悉尼雅思），成立于2012年，于2015年开始培训PTE，作为悉尼第一家PTE培训学校，我们是PTE评分系统的探索先驱，是PTE教学研究的开拓者，更是PTE真题预测的奠基者。多年的时间历练，让只用通过率说话的Easton，凭借其教学质量和教学成果，每年帮助上千名学生达成语言目标。2017年创立A WIN留学移民专家服务，恪守倾听，关心，专业，负责的态度，为学生的留学移民梦，填上了最后一块拼图。',
          },
          needLogin: true
        },
        component: () =>
          import(/* webpackChunkName: "practice" */ "@/views/practice/loged")
      },
      {
        path: "/user",
        meta: {
          title: "个人中心",
          needLogin: true
        },
        component: () => import(/* webpackChunkName: "User" */ "@/views/user"),
        children: [
          {
            path: "/userProfile",
            meta: {
              title: "个人资料",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "User" */ "@/views/user/userprofile")
          }, // 个人资料
          {
            path: "/viporder",
            meta: {
              title: "订单中心",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "order" */ "@/views/user/order")
          }, // 我的vip订单
          {
            path: "/courseorder",
            meta: {
              title: "课程订单",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "order" */ "@/views/user/courseorder")
          }, // 我的课程订单
          {
            path: "/mycourses",
            meta: {
              title: "我的课程",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/mycourses")
          }, // 我的课程
          {
            path: "/regcourse",
            meta: {
              title: "已报课程",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/regcourse")
          }, // 已报课程列表
          {
            path: "/regcourselist",
            meta: {
              title: "已报课程列表",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "user" */ "@/views/user/regcourselist"
              )
          }, // 已报课程列表
          {
            path: "/playback",
            meta: {
              title: "回放列表",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/playback")
          }, // 课程回放
          {
            path: "/record",
            meta: {
              title: "成绩记录",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/record")
          }, // 成绩记录
          {
            path: "/studyplan",
            meta: {
              title: "学习计划",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/studyplan")
          }, // 学习计划
          {
            path: "/myhomework",
            meta: {
              title: "我的作业本",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/myhomework")
          }, // 我的作业
          {
            path: "/checkhomework",
            meta: {
              title: "查看作业批改",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "user" */ "@/views/user/checkhomework"
              )
          }, // 查看作业批改
          {
            path: "/reviewhomework",
            meta: {
              title: "复习作业",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "user" */ "@/views/user/reviewhomework"
              )
          }, // 复习作业
          {
            path: "/message",
            meta: {
              title: "消息列表",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/message")
          }, // 我的消息
          {
            path: "/jjdownload",
            meta: {
              title: "机经下载",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "user" */ "@/views/user/jjdownload")
          }, // 机经下载
          {
            path: "/mbdownload",
            meta: {
              title: "资料下载",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "download" */ "@/views/user/mbdownload"
              )
          }, // 模板下载
          {
            path: "/survey",
            meta: {
              title: "问卷调查",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "survey" */ "@/views/user/survey")
          }, // 问卷调查
          {
            path: "/toVip",
            meta: {
              title: "开通VIP",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "order" */ "@/views/user/vip")
          }, // 开通会员
          {
            path: "/AIanalysis",
            meta: {
              title: "AI分析",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "survey" */ "@/views/user/AIanalysis")
          }, // AI分析
          {
            path: "/studyplaninfo",
            meta: {
              title: "学习计划详情",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "order" */ "@/views/user/studyplaninfo"
              )
          }, // 学习计划详情
          {
            path: "/userInfo",
            meta: {
              title: "个人中心",
              needLogin: true
            },
            component: () =>
              import(/* webpackChunkName: "User" */ "@/views/user/userinfo")
          }, // 个人中心
          {
            path: "/todo",
            meta: {
              title: "完成作业",
              needLogin: true
            },
            component: () =>
              import(
                /* webpackChunkName: "order" */ "@/views/user/todohomework"
              )
          } // 去完成作业
        ]
      }
    ]
  },
  {
    path: "/videoplay",
    meta: {
      title: "视频",
      needLogin: true
    },
    component: () =>
      import(/* webpackChunkName: "courses" */ "@/views/courses/videoplay")
  },
  {
    path: "/cate",
    meta: {
      title: "练习平台",
      needLogin: true
    },
    component: () =>
      import(
        /* webpackChunkName: "exercise" */ "@/views/practice/exercise/cate"
      )
  },
  // 练习平台路由
  {
    path: "/exercise",
    meta: {
      title: "习题"
    },
    component: () =>
      import(/* webpackChunkName: "exercise" */ "@/views/practice/exercise"),
    redirect: "/cate",
    children: [
      {
        path: "/cate",
        component: () =>
          import(
            /* webpackChunkName: "exercise" */ "@/views/practice/exercise/cate"
          )
      },
      {
        path: "/writingRA",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingRA" */ "@/views/practice/exercise/writing/RA"
          )
      },
      {
        path: "/writingRS",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingRS" */ "@/views/practice/exercise/writing/RS"
          )
      },
      {
        path: "/writingDI",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingDI" */ "@/views/practice/exercise/writing/DI"
          )
      },
      {
        path: "/writingRL",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingRL" */ "@/views/practice/exercise/writing/RL"
          )
      },
      {
        path: "/writingASQ",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingASQ" */ "@/views/practice/exercise/writing/ASQ"
          )
      },
      {
        path: "/readingRFIB",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingRFIB" */ "@/views/practice/exercise/reading/RFIB"
          )
      },
      {
        path: "/readingRP",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingRP" */ "@/views/practice/exercise/reading/RP"
          )
      },
      {
        path: "/readingFIBRW",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingFIBRW" */ "@/views/practice/exercise/reading/FIB_RW"
          )
      },
      {
        path: "/readingMCS",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingMCS" */ "@/views/practice/exercise/reading/MCS"
          )
      },
      {
        path: "/readingMCM",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingMCM" */ "@/views/practice/exercise/reading/MCM"
          )
      },

      {
        path: "/readingSMW",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "readingSMW" */ "@/views/practice/exercise/reading/SMW"
          )
      },
      {
        path: "/writingSWT",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingSWT" */ "@/views/practice/exercise/writing/SWT"
          )
      },
      {
        path: "/writingWE",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "writingWE" */ "@/views/practice/exercise/writing/WE"
          )
      },
      {
        path: "/listeningSST",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningSST" */ "@/views/practice/exercise/listening/SST"
          )
      },
      {
        path: "/listeningFIB",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningFIB" */ "@/views/practice/exercise/listening/FIB"
          )
      },
      {
        path: "/listeningHIW",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningHIW" */ "@/views/practice/exercise/listening/HIW"
          )
      },
      {
        path: "/listeningWFD",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningWFD" */ "@/views/practice/exercise/listening/WFD"
          )
      },
      {
        path: "/listeningMCM",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningMCM" */ "@/views/practice/exercise/listening/MCM"
          )
      },
      {
        path: "/listeningMCS",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningMCS" */ "@/views/practice/exercise/listening/MCS"
          )
      },
      {
        path: "/listeningHCS",
        meta: {
          title: "九炸炸英语",
          needLogin: true
        },
        component: () =>
          import(
            /* webpackChunkName: "listeningHCS" */ "@/views/practice/exercise/listening/HCS"
          )
      }
    ]
  },
  {
    path: "/practice",
    name: "Practice",
    meta: {
      title: "练习平台"
    },
    component: () =>
      import(/* webpackChunkName: "practice" */ "@/views/practice")
  },
  {
    path: "/loged",
    meta: {
      title: "练习平台"
    },
    // name: "loged",
    component: () =>
      import(/* webpackChunkName: "practice" */ "@/views/practice/loged")
  },
  {
    path: "/community",
    name: "community",
    meta: {
      title: "社区中心"
    },
    component: () =>
      import(/* webpackChunkName: "community" */ "@/views/community")
  },
  {
    path: "/login",
    meta: {
      title: "登录"
    },
    name: "login",
    component: () => import(/* webpackChunkName: "login" */ "@/views/login"),
    redirect: "/toLogin",
    children: [
      {
        path: "/toLogin",
        name: "toLogin",
        meta: {
          title: "登录"
        },
        component: () =>
          import(/* webpackChunkName: "login" */ "@/views/login/toLogin")
      },
      {
        path: "/toReg",
        name: "toReg",
        meta: {
          title: "注册"
        },
        component: () =>
          import(/* webpackChunkName: "login" */ "@/views/login/toReg")
      },
      {
        path: "/forgetpwd",
        name: "forgetpwd",
        meta: {
          title: "忘记密码"
        },
        component: () =>
          import(/* webpackChunkName: "login" */ "@/views/login/forgetpwd")
      },
      {
        path: "/scanLogin",
        name: "scanLogin",
        meta: {
          title: "扫码登录"
        },
        component: () =>
          import(/* webpackChunkName: "login" */ "@/views/login/scanLogin")
      }
    ]
  }
];

const router = new VueRouter({
  mode:'history',
  base: '/easton/',
  routes,
});

// // 添加路由守卫、导航守卫
// router.beforeEach((to, from, next) => {
//   // 如果用户访问的是课程首页，直接让用户访问即可
//   if (to.path === "/courses") return next();

//   const tokenStr = sessionStorage.getItem("token");

//   // 判断本地有没有 token，如果没有 token，需要跳转到登录页面，让用户进行登录
//   if (!tokenStr) return next();

//   // 如果本地存在 token，用户访问哪个路由，就立即跳转即可
//   next();
// });

// 添加路由守卫、导航守卫
router.beforeEach((to, from, next) => {
  let { title, needLogin } = to.meta;
  let isLogin = userUtils.isLogin();


  if(to.meta.content){
    let head = document.getElementsByTagName('head');
    let meta = document.createElement('meta');
    document.querySelector('meta[name="keywords"]').setAttribute('content', to.meta.content.keywords)
    document.querySelector('meta[name="description"]').setAttribute('content', to.meta.content.description)
    meta.content = to.meta.content;
    head[0].appendChild(meta)
  }
  // /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

 

  if (needLogin && !isLogin) {
    next({
      path: "/Login",
      // query参数是为了设置登陆后路由重定向到之前访问的路由的
      query: {
        redirect: to.fullPath,
        hostname: location.hostname
      }
    });
  } else {
    next();
  }
});

export default router;
